package com.example.common.utils;

/**
 * 统一API响应状态码枚举
 * 用于全项目接口的标准状态码和消息
 */
public enum ResultCode implements IErrorCode {
    SUCCESS(200, "操作成功"),
    FAIL(501, "操作失败"),
    ERROR(500, "系统发生错误"),
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未登录或token已过期"),
    FORBIDDEN(403, "没有相关权限"),
    NOT_FOUND(404, "资源未找到");

    private final int code;
    private final String message;

    ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    /**
     * 根据状态码查找对应的 ResultCode 枚举实例
     * @param code 状态码
     * @return 对应的 ResultCode 枚举实例，如果未找到则返回 ERROR
     */
    public static ResultCode fromCode(int code) {
        for (ResultCode value : values()) {
            if (value.code == code) {
                return value;
            }
        }
        return ERROR;
    }
} 